layui.use(['table', 'admin', 'ax', 'ztree', 'laydate'], function () {
    var form = layui.form;
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var $ZTree = layui.ztree;
    var laydate = layui.laydate;

    /**
     * 产品管理初始化
     */
    var Obj = {
        tableId: "productTable",	//表格id
        condition: {
            name: ""
        }
    };
    //渲染时间选择框
    laydate.render({
        elem: '#timeLimit',
        range: true,
        max: Feng.currentDate()
    });
    /**
     * 初始化表格的列
     */
    Obj.initColumn = function () {
        return [[
            // {type: 'checkbox'},
            {field: 'id', hide: true, sort: true, title: 'id'},
            {field: 'logoUrl', sort: true, title: '图标', templet: '#logoUrlTpl', width: 80},
            {field: 'name', sort: true, title: '名称', width: 100},
            {field: 'pledgeCoins', sort: true, title: '质押币种'},
            {field: 'totalReward', sort: true, title: '每次分红总金额'},
            {field: 'startTime', sort: true, title: '开始时间', width: 180},
            {field: 'cycleDay', sort: true, title: '分红天数'},
            {field: 'endTime', sort: true, title: '结束时间', width: 180},
            {field: 'intervalTime', sort: true, title: '分红间隔(分钟)'},
            {field: 'sortNum', sort: true, title: '排序', width: 80},
            {field: 'createTime', sort: true, title: '创建时间', width: 180},
            {field: 'updateTime', sort: true, title: '修改时间', width: 180},
            {field: 'isHot', fixed: 'right', templet: '#isHotTpl', title: '热门', width: 100},
            {field: 'enableFlag', fixed: 'right', templet: '#enableFlagTpl', title: '状态', width: 100},
            {align: 'center', fixed: 'right', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    form.on('switch(enableFlag)', function (obj) {
        var id = obj.elem.value;
        var checked = obj.elem.checked ? true : false;
        Obj.changeEnableFlag(id, checked);
    });

    form.on('switch(isHot)', function (obj) {
        var id = obj.elem.value;
        var checked = obj.elem.checked ? true : false;
        Obj.changeIsHot(id, checked);
    });

    Obj.changeIsHot = function (id, checked) {
        let url = Feng.ctxPath + "/product/changeIsHot/N"
        if (checked) {
            url = Feng.ctxPath + "/product/changeIsHot/Y"
        }
        var ajax = new $ax(url, function (data) {
            if (checked) {
                Feng.success("已启用!");
            } else {
                Feng.success("已禁用!");
            }
        }, function (data) {
            Feng.error("失败!");
            table.reload(Obj.tableId);
        });
        ajax.set("id", id);
        ajax.start();
    }

    Obj.changeEnableFlag = function (id, checked) {
        let url = Feng.ctxPath + "/product/changeEnableFlag/N"
        if (checked) {
            url = Feng.ctxPath + "/product/changeEnableFlag/Y"
        }
        var ajax = new $ax(url, function (data) {
            if (checked) {
                Feng.success("已启用!");
            } else {
                Feng.success("已禁用!");
            }
        }, function (data) {
            Feng.error("失败!");
            table.reload(Obj.tableId);
        });
        ajax.set("id", id);
        ajax.start();
    };


    /**
     * 点击查询按钮
     */
    Obj.search = function () {
        var queryData = {};
        queryData['name'] = $("#name").val();
        table.reload(Obj.tableId, {where: queryData});
    };


    /**
     * 打开查看产品详情
     */
    Obj.openProductDetail = function () {
        if (this.check()) {
            var index = layer.open({
                type: 2,
                maxmin: true,
                title: '产品详情',
                area: ['800px', '420px'], //宽高
                fix: false, //不固定
                maxmin: true,
                content: Feng.ctxPath + '/product/product_edit/' + Obj.seItem.id
            });
            this.layerIndex = index;
        }
    };


    /**
     * 删除产品
     *
     */
    Obj.onDeleteProduct = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/product/delete", function (res) {
                if (res.code != 200) {
                    Feng.error("删除失败!" + res.msg + "!");
                } else {
                    Feng.success("删除成功!");
                }
                table.reload(Obj.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除产品 ?", operation);
    };

    /**
     * 弹出添加产品
     */
    Obj.openAddProduct = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            maxmin: true,
            title: '添加产品',
            area: ["800px", "420px"],
            content: Feng.ctxPath + '/product/product_add',
            end: function () {
                admin.getTempData('formOk') && table.reload(Obj.tableId);
            }
        });
    };

    /**
     * 导出excel按钮
     */
    Obj.exportExcel = function () {
        var checkRows = table.checkStatus(Obj.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };
    /**
     * 点击编辑产品
     *
     */
    Obj.onEditProduct = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            maxmin: true,
            title: '修改产品',
            area: ["800px", "420px"],
            content: Feng.ctxPath + '/product/product_edit?productId=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(Obj.tableId);
            }
        });
    };
    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Obj.tableId,
        url: Feng.ctxPath + '/product/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        //limit:100,
        //limits: [100,200,300,400,500],
        parseData: function (res) {
            return {
                "code": 0,
                "msg": res.msg,
                "count": res.data.total,
                "data": res.data.records
            };
        },
        cols: Obj.initColumn()
    });
    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Obj.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Obj.openAddProduct();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Obj.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Obj.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'edit') {
            Obj.onEditProduct(data);
        } else if (layEvent === 'delete') {
            Obj.onDeleteProduct(data);
        }
    });
});
